<template>
  <div>
    <el-card class="top"><el-header>加息券详情</el-header></el-card>

    <!-- 表格展示 -->
    <el-card>
      <el-input placeholder="加息券名称" prefix-icon="el-icon-search" style="width: 200px" v-model="name">
      </el-input>
      <el-button type="warning" @click="reload" plain>查询</el-button>
      <el-button type="warning" @click="addformVisible = true" v-if="canEditAndDelete">新增加息券信息</el-button>
      <el-table :data="vouchersList" style="width: 100%" :sort-by.sync="sortKey"
        :sort-orders="['ascending', 'descending']">
        <el-table-column type="index" label="编号" width="50" align="center">
        </el-table-column>
        <el-table-column prop="name" label="加息券名称" width="100" align="center">
        </el-table-column>
        <el-table-column prop="increase" label="加息券利率(%)" width="120" align="center">
        </el-table-column>
        <el-table-column prop="money" label="使用金额起始值(元)" width="170" align="center" sortable>
        </el-table-column>
        <el-table-column prop="start" label="期限起始值(天)" width="120" align="center">
        </el-table-column>
        <el-table-column prop="time" label="加息时长(天)" width="110" align="center">
        </el-table-column>
        <el-table-column prop="validity" label="有效期(天)" width="100" align="center">
        </el-table-column>
        <el-table-column prop="type" label="产品类型限制" width="110" align="center">
        </el-table-column>
        <el-table-column prop="num" label="总数" width="80" align="center" sortable>
        </el-table-column>
        <el-table-column prop="usetype" label="商品状态" width="120" align="center" sortable>
          <template slot-scope="scope">
            <span v-if="scope.row.usetype
        == 0">已下架</span>
            <span v-if="scope.row.usetype
        == 1">启用</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="150">
          <template slot-scope="scope">
            <el-button v-if="canEditAndDelete" size="mini" type="primary"
              @click="editformVisible = true, handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleget(scope.row)">领取</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 新增 -->
    <el-dialog title="新增加息券信息" :visible.sync="addformVisible" :modal-append-to-body="false">
      <el-form :model="addform">
        <el-form-item label="加息券名称" :label-width="formLabelWidth">
          <el-input v-model="addform.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="使用金额起始值" :label-width="formLabelWidth">
          <el-input v-model="addform.money" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="发放个数" :label-width="formLabelWidth">
          <el-input v-model="addform.num" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addformVisible = false">取 消</el-button>
        <el-button type="primary" @click="add()">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 编辑 -->
    <el-dialog title="编辑加息券信息" :visible.sync="editformVisible" :modal-append-to-body="false">
      <el-form :model="editform">
        <el-form-item label="加息券名称" :label-width="formLabelWidth">
          <el-input v-model="editform.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="使用金额起始值" :label-width="formLabelWidth">
          <el-input v-model="editform.money" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="发放个数" :label-width="formLabelWidth">
          <el-input v-model="editform.num" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-radio v-model="editform.usetype
" label="1">启用</el-radio>
          <el-radio v-model="editform.usetype
" label="0">下架</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editformVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit()">确 定</el-button>
      </div>
    </el-dialog>


    <!--    分页-->
    <el-row>
      <el-col style="text-align:right">
        <el-pagination :current-page="currentPage" @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :page-size="pageSize" :page-sizes="[5, 10, 15]" layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </el-col>
    </el-row>
  </div>

</template>

<script>
import { del } from 'vue';

export default {
  data() {
    return {
      uid: "",
      name: "",
      rolename: '',
      vouchersList: [],
      getVouchersfrom: {
        uid: '',
        vid: '',
      },
      addformVisible: false,
      addform: {},
      editformVisible: false,
      editform: {
        id: '',
        name: '',
        money: '',
        num: '',
        usetype
: '',
      },
      timevalue: [new Date(), new Date()],
      formLabelWidth: '120px',
      //配置分页
      currentPage: 1,
      pageSize: 5,
      total: 0,
      sortKey: ''
    };
  },

  created() {
    const storedUserInfo = localStorage.getItem('user');
    if (storedUserInfo) {
      this.user = JSON.parse(storedUserInfo);
      this.rolename = this.user.roleList[0].name;
      console.log(this.rolename);
    }
  },

  computed: {
    // 根据用户角色决定是否显示编辑和删除按钮
    canEditAndDelete() {
      const rolenames = ['超级管理员', '产品经理', '后台管理员', '客服经理'];
      return rolenames.includes(this.rolename); // 只有指定角色可以编辑和删除
    }
  },

  mounted() {
    this.reload();

    if (localStorage.getItem("user")) {
      this.user = JSON.parse(localStorage.getItem("user"))
      this.uid = this.user.uid;
    }
  },

  methods: {
    //领取
    handleget(row) {
      this.getVouchersfrom.uid = this.uid;
      this.getVouchersfrom.vid = row.id;
      this.$axios.post("/vouchers/getVouchers", this.getVouchersfrom).then(res => {
        console.log(res);
        if (res.data.data == true) {
          this.$message({
            message: "领取成功",
            type: 'success'
          });
          this.reload();
        } else if (res.data.data == false || row.usetype == 0) {
          this.$message({
            message: "领取失败",
            type: 'error'
          });
          this.reload();
        }
      })
    },

    //添加
    add() {
      this.$axios.post("/vouchers/addVouchers", this.addform).then(res => {
        console.log(res);
        if (res.data.code == 200) {
          //对话框消失
          this.addformVisible = false;
          //清空新增留存上一个数据
          this.addform = {};
          //给出添加成功或者失败提示
          this.$message({
            message: "添加成功",
            type: 'success'
          });
          this.reload();
        } else {
          this.addformVisible = false
          this.$message({
            message: "添加失败",
            type: 'error'
          });
        }
      })
    },

    // 编辑
    edit(row) {
      this.editformVisible = true
      Object.assign(this.editform, row)
      this.$axios.put("/vouchers/updateVouchers", this.editform).then(res => {
        console.log(res.data);
        if (res.data.code == 200) {
          this.$message({
            message: "编辑成功",
            type: 'success'
          });
          this.editformVisible = false
        } else {
          this.$message.console.error("编辑失败");
        }
        this.reload();
      })
    },

    // 删除
    del(id) {
      this.$axios.delete("/KillVouchers/delete/" + id).then(res => {
        if (res.data.code == 200) {
          this.$message({
            message: "删除成功",
            type: 'success'
          });
          this.reload();
        } else {
          this.$message.console.error("删除失败");
        }
      })
    },

    reload() {
      this.$axios.get("/vouchers/findAll?currentPage=" + this.currentPage + "&pageSize=" + this.pageSize + "&name=" + this.name).then((res) => {
        console.log(res.data.data);
        this.vouchersList = res.data.data.list;
        this.total = res.data.data.total;
      });
    },

    handleEdit(row) {
      this.editform = { ...row };
    },


    handleChange(value) {
      console.log(value);
    },

    //显示查询出的总条数
    handleSizeChange(val) {
      this.pageSize = val;
      this.reload();
    },

    //点击新页查询的数据
    handleCurrentChange(val) {
      this.currentPage = val;
      this.reload();
    },

    changeTime() {
      this.addform.starttime = this.timevalue[0]
      this.addform.endtime = this.timevalue[1]
    }
  },
};
</script>

<style scoped>
.el-header {
  text-align: center;
  line-height: 30px;
  size: 120px;
}

.top {
  background-color: #D3D3D3;
  height: 60px;
}

.el-card {
  margin-top: 10px;
  margin-left: 30px;
}

.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
</style>